Previous | Home | Next |
Tag description: JSF <f:converter> Tag is register a Converter instance on UIcomponent associate with the enclosing parent tag.
Interface Converter, describing a Java class that can perform String-to-Object or Object-to-String conversions between model data objects and a String representation of those objects that is suitable for rendering.
Code
<f:converter>
Example :
Step 1: Welcome page of Example
<%-- Name= welcomeJSF --%> <%@page contentType="text/html" pageEncoding="UTF-8"%> <%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%> <%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%-- This file is an entry point for JavaServer Faces application. --%> <f:view> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> <title>r4r.co.in</title> </head> <body> <h1><h:outputText value="Tag <f:converter> Example"/></h1> <h:form id="converter"> <%--Display Error message --%> <h:messages for="converter:name" globalOnly="true" style="color:red" /> <h:panelGrid columns="2" cellpadding="2" cellspacing="2" > <h:outputLabel value="Website name" /> <h:inputText id="name" value="#{converter.name}" required="true" size="30"> <f:converter converterId="ConverterClass" /> </h:inputText> <h:commandButton action="#{converter.submit()}" value="Submit" /> </h:panelGrid> <br><br> <%-- Display result --%> <h:panelGrid rendered="#{converter.flag != false}" > Website url: <h:outputText value="#{converter.name}"/> </h:panelGrid> </h:form> </body> </html> </f:view>
Step 2: Faces-config.xml file for mapping resource
<?xml version='1.0' encoding='UTF-8'?> <!-- =========== FULL CONFIGURATION FILE ========= --> <faces-config version="2.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"> <managed-bean> <managed-bean-name>converterBean</managed-bean-name> <managed-bean-class>r4r.JSF2.converterBean</managed-bean-class> <managed-bean-scope>request</managed-bean-scope> </managed-bean> <!-- == Add Converter class invoked by converterId in JSP == --> <converter> <converter-id>ConverterClass</converter-id> <converter-class>r4r.JSF2.ConverterClass</converter-class> </converter> </faces-config>
Step 3: ManagedBean class for provide logic in program
/* * Save as a converterBean.java */ package r4r.JSF2; import javax.faces.bean.ManagedBean; /* --Bean name -- */ @ManagedBean(name = "converter") public class converterBean { private String name; private boolean flag = false; /* -- Getter/Setter -- */ public boolean isFlag() { return flag; } public void setFlag(boolean flag) { this.flag = flag; } public String getName() { return name; } public void setName(String name) { this.name = name; } /* -- submit method -- */ public String submit() { flag = true; return "submit"; } }
Step 4: Converter class
/* * Save as a ConverterClass.java * Class is implements Converter interface that can perform * Object-to-String and String-to-Object conversions */ package r4r.JSF2; import javax.faces.application.FacesMessage; import javax.faces.component.UIComponent; import javax.faces.context.FacesContext; import javax.faces.convert.Converter; import javax.faces.convert.FacesConverter; import org.hibernate.validator.constraints.impl.URLValidator; @FacesConverter public class ConverterClass implements Converter { @Override public Object getAsObject(FacesContext context, UIComponent component, String value) { /* -- define string -- */ String HTTP = "http://www."; String COM = ".com"; StringBuilder url = new StringBuilder(); /* -- Check input String start with http in front, if not then add it -- */ if (!value.startsWith(HTTP, 0)) { url.append(HTTP); } /* -- Check input String contain .com in last, if not then add it -- */ if (value.indexOf(".com") == -1) { url.append(value).append(COM); } //use common URL validator to validate URL URLValidator uRLValidator = new URLValidator(); /*-- Check the URL validation --*/ if (uRLValidator.isValid(url.toString(), null)) { FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "URL conversion error!", "enter URL format is invalid"); context.addMessage(null, message); } /* -- Create a new NewURL url class which extends object-- */ NewURL newURL = new NewURL(url.toString()); return newURL; } @Override public String getAsString(FacesContext context, UIComponent component, Object value) { return value.toString(); } }
step 5: NewURL class
/* * Save as a NewURL.java * Class in used to set/get the NewURL property . */ package r4r.JSF2; public class NewURL { private String NewFullURL; NewURL(String NewFullURL) { this.NewFullURL = NewFullURL; } /* -- Getter/Setter -- */ public String getNewFullURL() { return NewFullURL; } public void setNewFullURL(String NewFullURL) { this.NewFullURL = NewFullURL; } @Override public String toString() { return NewFullURL.toString(); } }
Output:
Previous | Home | Next |